<?php
/**
 * 异步发送邮件
 * @author chenjunliamg@star-net.cn
 * @copyright  ktvme. All Rights Reserved.
 * @version $Id: Asynemail.php 497 2018-01-26 07:09:23Z chendanming $
 */

namespace app\console\service;


use app\console\common\ClientCommand;

class Asynemail {
    /**
     * 向swoole发送任务,
     * @param null $params
     * 参数:
     * reciever :收件人 数组 可配置多个收件人
     * sender   :发件人邮箱 String
     * password :发件人邮箱密码
     * subject  :邮件主题
     * content  :邮件内容
     */
    public static function asynSendEmail($params = null) {
        //        判断是否发送邮件
        if(!config('email.permission')) {
            return false;
        }
        $data = [
            'reciever' => config('email.reciever'),
            'sender'   => config('email.sender'),
            'password' => config('email.password'),
            'subject'  => config('email.subject'),
            'content'  => config('email.content'),
        ];
        if(isset($params['reciever']) && $params['reciever']) {
            $data['reciever'] = $params['reciever'];
        }

        if(isset($params['subject']) && $params['subject']) {
            $data['subject'] = $params['subject'];
        }

        if(isset($params['content']) && $params['content']) {
            $data['content'] = $params['content'];
        }
        $email  = [
            'type' => 'Email',
            'data' => $data,
        ];
        $client = new ClientCommand();
        $send   = $client->sendData(json_encode($email));

        return $send;
    }


    /**
     * 测试版
     * 向swoole发送任务
     * @param null $params
     * 参数:
     * reciever :收件人 数组 可配置多个收件人
     * sender   :发件人邮箱 String
     * password :发件人邮箱密码
     * subject  :邮件主题
     * content  :邮件内容
     */
    public static function asynSendEmailTest($params = null) {
        $data   = [
            'reciever' => $params['reciever'],
            'sender'   => $params['sender'],
            'password' => $params['password'],
            'subject'  => $params['subject'],
            'content'  => $params['content'],
        ];
        $email  = [
            'type' => 'Email',
            'data' => $data,
        ];
        $client = new ClientCommand();
        $client->sendData(json_encode($email));

        return true;
    }
}